package com.csyaonie.android;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.csyaonie.android.utils.ToastUtil;

/**
 * 五种实现事件监听的方法
 */
public class EventActivity extends AppCompatActivity implements View.OnClickListener{

    private Button bt_event;
    private Button bt_event2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        bt_event=findViewById(R.id.bt_event);
        bt_event2=findViewById(R.id.bt_event2);

        //匿名内部类
        bt_event.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show(EventActivity.this,"匿名内部类");
            }
        });
        //内部类
        bt_event.setOnClickListener(new OnClick());
        //实现接口
        bt_event.setOnClickListener(EventActivity.this);
        //外部类
        bt_event.setOnClickListener(new OnClickClass(EventActivity.this));

        //测试事件回调
        bt_event2.findViewById(R.id.bt_event2);
        bt_event2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.d("Listener", "onTouch");
                        break;
                }
                return false;
            }
        });

    }

    public class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bt_event:
                    Toast.makeText(EventActivity.this,"内部类",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_event:
                Toast.makeText(EventActivity.this,"实现事件接口",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public void onBtClick(View v){
        switch (v.getId()){
            case R.id.bt_event:
                Toast.makeText(EventActivity.this,"布局监听事件",Toast.LENGTH_SHORT);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("Activity", "onTouchEvent");
                break;
        }
        return super.onTouchEvent(event);
    }
}

class OnClickClass implements View.OnClickListener{
    private Activity mActivity;

    public OnClickClass(Activity activity){
        this.mActivity=activity;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_event:
                Toast.makeText(mActivity,"外部类",Toast.LENGTH_SHORT).show();
        }
    }
}